package com.gateway.config;

import com.gateway.filter.SecurityContextConverterFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @author 罗俊华
 * @date 2022/3/14 - 4:44 下午
 */
@Slf4j
@Configuration
public class GatewayFilterConfig {


    @Bean
    public GlobalFilter globalFilter(){
        return new SecurityContextConverterFilter();
    }

    /**
     * 配置跨域
     * @return
     * @deprecated 最新网关跨域配置，请参考 https://docs.spring.io/spring-cloud-gateway/docs/2.2.9.RELEASE/reference/html/#cors-configuration
     */
    /*@Bean
    public FilterRegistrationBean<CorsFilter> filterRegistrationBean(){

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        CorsConfiguration corsConfiguration = new CorsConfiguration();

        corsConfiguration.setAllowCredentials(true);

        corsConfiguration.addAllowedOrigin("*");

        corsConfiguration.addAllowedHeader("*");

        corsConfiguration.addAllowedMethod("*");

        corsConfiguration.setMaxAge(18000L);

        source.registerCorsConfiguration("/**",corsConfiguration);

        CorsFilter corsFilter = new CorsFilter(source);

        FilterRegistrationBean<CorsFilter> corsFilterFilterRegistrationBean = new FilterRegistrationBean<>(corsFilter);

        corsFilterFilterRegistrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);

        return corsFilterFilterRegistrationBean;

    }*/
}
